public class JavaKE2{
    public static void main(String[] args){

        //顺序结构
        int a=10;
        int b=20;
        int sum,difference,product;
        sum=a+b;
        difference=a-b;
        product=a*b;
        System.out.println(a);
        System.out.println(b);
        System.out.println(sum);
        System.out.println(difference);
        System.out.println(product);
        //方法调用
        printMessage();
        System.out.println("\n程序执行完毕");
    }
    //自定义方法
    public static void printMessage() {
        System.out.println("\n这是从printMessage方法中打印的信息");
        System.out.println("其中的代码按顺序执行");
        String message = "Hello,Java!";
        int length = message.length();
        System.out.println(length);


        //分支结构
        int score = 85;
        //if
        if (score >= 60) {
            System.out.println("成绩及格"); // 当条件为true时执行
        }
        // if-else 语句 - 二选一分支
        if (score >= 90) {
            System.out.println("优秀");
        } else {
            System.out.println("未达到优秀"); // 当if条件为false时执行
        }
        // if-else if-else 语句 - 多条件分支
        if (score >= 90) {
            System.out.println("等级A");
        } else if (score >= 80) { // 前一个条件为false时检查这个条件
            System.out.println("等级B");
        } else if (score >= 70) {
            System.out.println("等级C");
        } else if (score >= 60) {
            System.out.println("等级D");
        } else {
            System.out.println("等级E"); // 所有条件都为false时执行
        }
        //if嵌套
        int age = 25;
        boolean hasLicense = true;
        if (age >= 18) {
            System.out.println("年龄达标");
            if (hasLicense) {//外层if为true时才检查这个条件
                System.out.println("可以驾驶");
            } else {
                System.out.println("需要先取得驾照");
            }
        } else {
            System.out.println("年龄未达标，不能驾驶");
        }
        //switch
        char grade = 'B';
        switch (grade) {
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("不及格");
                break;
            default:
                System.out.println("无效等级");
        }
        //switch表达式  可以有返回值
        String day = "Monday";
        String dayType = switch (day) {
            case "Saturday", "Sunday" -> "周末";//多case匹配
            case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "工作日";
            default -> "未知";//返回值赋给dayType
        };
        System.out.println(dayType);
        //三元运算符
        int num1 = 10, num2 = 20;
        int max = (num1 > num2) ? num1 : num2;
        System.out.println(max);


        //循环结构
        //for循环
        for (int i = 0; i < 5; i++) {
            System.out.println(i);
        }
        //增强for循环-遍历数组
        int[] numbers = {10, 20, 30, 40, 50};
        for (int num : numbers) {
            System.out.println(num);
        }
        //wwhile循环-先判断条件再执行
        int j = 0;
        while (j < 3) {
            System.out.println(j);
            j++;
        }
        //do-while循环-先执行一次再判断条件
        int k = 0;
        do {
            System.out.println(k);
            k++;
        } while (k < 3);
        //循环控制语句
        for (int m = 0; m < 10; m++) {
            if (m == 2) {
                continue;
            }
            if (m == 7) {
                break;
            }
            System.out.println(m);
        }
        //嵌套循环
        for (int x = 1; x <= 3; x++) {
            for (int y = 1; y <= 3; y++) {
                System.out.println(x + "," + y);
            }
        }
        //无限循环
        int count = 0;
        while (true) {
            System.out.println("这是无限循环的第" + (count++) + "次迭代");
            if (count >= 3) {
                break;
            }
        }



        //跳转语句return,只要执行到return语句，方法就会结束
        //有返回值
        System.out.println(returnFive());
        System.out.println(earlyReturn(true));
        System.out.println(earlyReturn(false));
        //无返回值
        printNumber(5);
        printNumber(-1);
    }
        //return有返回值
        private static int returnFive(){
            return 5;
        }
        private static String earlyReturn(boolean flag){
            if (flag){
                return "提前返回" ;
            }
            System.out.println("执行了方法的剩余部分");
            return "正常返回";
        }
        //return无返回值（void）
        private static void printNumber(int num){
            if (num<0){
                System.out.println("数字无效，提前返回");
                return;//提前结束方法执行
            }
            System.out.println(num);
        }
        

}

